var word="";
PageClick = function(pageclickednumber, p,dw) {
	var startIndex = p * (pageclickednumber - 1);
	if(dw!=undefined)word=dw;
	$.ajax( {
		type : "POST",
		url : "admin_work.do?method=findDesigns",
		data : "startIndex=" + startIndex + "&pageSize=" + p+"&word="+word,
		dataType : "json",
		success : function(data) {
			var totalCount = data.totalCount;
			var pageSize = data.pageSize;
			var currentPage = data.currentPage;
			if (data.items != undefined) {
				showTable(data);
				findZbpls(totalCount, pageSize, currentPage);
			}else{
				$('.move').remove();
				$('#btn5').remove();
				$("#mypage").empty();
				alert("无数据信息");
			}
		},
		error : function() {
			alert("请求出错");
		}
	});
}
function findZbpls(count, p, pageclickednumber) {
	$('#mypage').mypagination(count, {
		callback : PageClick
	}, p, pageclickednumber - 1);
}
function showTable(data){
	var items=data.items;
	if(items!=0){
		$('.move').remove();
		$('#btn5').remove();
		$.each(items,function(i,n){
			var id=n.id;
			var s='<a style=cursor: pointer; onclick=deleteDesign('+data.currentPage+',"'+id+'",'+data.pageSize+','+data.totalCount+')>删除</a>';
			var zd=n.zd;
			var zd_url="";
			if(zd==1)zd_url='<a style="cursor: pointer" onclick=updateZd('+data.currentPage+','+data.pageSize+',"'+n.id+'")>取消置顶</a>';
			else zd_url='<a style="cursor: pointer" onclick=updateZd('+data.currentPage+','+data.pageSize+',"'+n.id+'")>置顶</a>';
			if(i%2==0){
				var tr='<tr class="move">'
							+'<td align="center" valign="middle" bordercolor="#E7E7E7" bgcolor="#E7E7E7"><input type="checkbox" name="checkbox" id="checkbox" value="'+n.id+'"/></td>'
							+'<td height="23" align="center" valign="middle" bordercolor="#E7E7E7" bgcolor="#E7E7E7">'+n.name+'</td>'
							+'<td height="23" align="center" valign="middle" bordercolor="#E7E7E7" bgcolor="#E7E7E7">'+n.varcharge+' 元/平米</td>'
							+'<td height="23" align="center" valign="middle" bordercolor="#E7E7E7" bgcolor="#E7E7E7">'+n.rank+'</td>'
							+'<td height="23" align="center" valign="middle" bordercolor="#E7E7E7" bgcolor="#E7E7E7">'+n.city+'</td>'
							+'<td height="23" align="center" valign="middle" bordercolor="#E7E7E7" bgcolor="#E7E7E7">'+n.style+'</td>'
							+'<td height="23" align="center" valign="middle" bordercolor="#E7E7E7" bgcolor="#E7E7E7"><a style="cursor: pointer" href="admin_work.do?method=lookDesigns&str='+n.id+'&currentPage='+data.currentPage+'&pageSize='+data.pageSize+'&page=lookDes&backpage=designerMessage">查看</a>|<a style="cursor: pointer" href="admin_work.do?method=lookDesigns&str='+n.id+'&currentPage='+data.currentPage+'&pageSize='+data.pageSize+'&page=anddesign">编辑</a>|'+zd_url+'|<a style="cursor: pointer"; onclick=deleteDesign('+data.currentPage+',"'+n.id+'",'+data.pageSize+','+data.totalCount+')>删除</a>|<span class="yle STYLE5"><a href="admin/works.jsp?str='+n.id+'">作品集</a></span></td>'
					+'</tr>';
				$("#datas").append(tr);
			}else{
				var tr='<tr class="move">'
						+'<td align="center" valign="middle"><input type="checkbox" name="checkbox" id="checkbox" value="'+n.id+'"/></td>'
						+'<td height="23" align="center" valign="middle">'+n.name+'</a></td>'
						+'<td height="23" align="center" valign="middle">'+n.varcharge+' 元/平米</td>'
						+'<td height="23" align="center" valign="middle">'+n.rank+'</td>'
						+'<td height="23" align="center" valign="middle">'+n.city+'</td>'
						+'<td height="23" align="center" valign="middle">'+n.style+'</td>'
						+'<td height="23" align="center" valign="middle"><a style="cursor: pointer" href="admin_work.do?method=lookDesigns&str='+n.id+'&currentPage='+data.currentPage+'&pageSize='+data.pageSize+'&page=lookDes&backpage=designerMessage">查看</a>|<a style="cursor: pointer" href="admin_work.do?method=lookDesigns&str='+n.id+'&currentPage='+data.currentPage+'&pageSize='+data.pageSize+'&page=anddesign">编辑</a>|'+zd_url+'|<a style="cursor: pointer"; onclick=deleteDesign('+data.currentPage+',"'+id+'",'+data.pageSize+','+data.totalCount+')>删除</a>|<span class="yle STYLE5"><a href="admin/works.jsp?str='+n.id+'">作品集</a></span></td>'
					+'</tr>';
				$("#datas").append(tr);
			}
			var tr='<tr class="move">'
						+'<td height="12" align="center" valign="middle">&nbsp;</td>'
						+'<td height="12" align="center" valign="middle">&nbsp;</td>'
						+'<td height="12" align="center" valign="middle">&nbsp;</td>'
						+'<td height="12" align="center" valign="middle">&nbsp;</td>'
						+'<td height="12" align="center" valign="middle">&nbsp;</td>'
						+'<td height="12" align="center" valign="middle">&nbsp;</td>'
						+'<td height="12" align="center" valign="middle">&nbsp;</td>'
				   +'</tr>';
			$("#datas").append(tr);
		});
		var input='<input type="button" id="btn5" value="删除所选" onclick="deleteNew('+data.currentPage+','+data.pageSize+','+data.totalCount+')"/>';
		$("#button").append(input);
	}
}
function deleteNew(currentPage,pageSize,totalCount){
	var pageNum=Math.ceil(totalCount/pageSize);
	var str="";
	var i=0;
	$(":checkbox[checked]").each(function(){
        str+=$(this).val()+",";
        i=i+1;
    })
    if(i==0){
    	alert("至少选择一条数据");
    	return false;
    }
    str=str.substring(0,str.length-1);
    var url="admin_work.do?method=deleteDesigns";
	$.post(url,"str="+str,function(data){
		if(data==-1){
			alert("删除失败");
		}else if(data==0){
			if(currentPage==pageNum&&(totalCount%pageSize==i||pageSize==1)){
				currentPage=currentPage-1;
			}
			if(currentPage==0)currentPage=1;
			PageClick(currentPage,pageSize,word);
			alert("删除成功");
		}
	},"json");
}
function deleteDesign(currentPage,str,pageSize,totalCount){
	var pageNum=Math.ceil(totalCount/pageSize);
	var url="admin_work.do?method=deleteDesign";
	$.post(url,"str="+str,function(data){
		if(data==-1){
			alert("请求出错");
		}else if(data==0){
			if(currentPage==pageNum&&(totalCount%pageSize==1||pageSize==1)){
				currentPage=currentPage-1;
			}
			if(currentPage==0)currentPage=1;
			PageClick(currentPage,pageSize,word);
			alert("删除成功");
		}
	},"json");
}
function updateZd(currentPage,pageSize,id){
	var url="admin_work.do?method=updateZd";
	$.getJSON(url, { str: id }, function(data){
		if(data==-1)alert("请求出错");
		else if(data==0)alert("置顶人数已满");
		else {
			if(data==1)alert("置顶成功");
			if(data==2)alert("取消置顶成功");
			PageClick(currentPage,pageSize,word);
		}
	});
}